# -*- coding: utf-8 -*-
# from odoo import http
from odoo import http
import io
from odoo.tools.misc import xlwt
import json
from pprint import pprint
from io import BytesIO
from odoo import http, _
from odoo.exceptions import UserError
from odoo.http import request
import  datetime
class SaleOrder(http.Controller):

    @http.route(['/hr_overtime/export_hr_overtime/'], type='http', auth='user',
                multilang=True)
    def export_purchase_order(self, **kwargs):
        date_start = kwargs.get('date_start')
        date_stop = kwargs.get('date_stop')
        employee_ids = kwargs.get('employee_ids')
        employee_ids = json.loads(employee_ids)
        #
        # data = json.loads(data['options'])
        # data=json.loads(data['days'])
        if employee_ids:
            date_start=datetime.datetime.strptime(date_start, '%Y-%m-%d')
            date_start_1=date_start
            date_stop=datetime.datetime.strptime(date_stop, '%Y-%m-%d')

            days = []
            descs=[]
            while date_start <= date_stop:
                day=date_start.strftime('%Y-%m-%d')
                desc=str(int(date_start.strftime('%Y-%m-%d')[-2:]))+'日'
                days.append(day)
                descs.append(desc)
                # days.append({'day':day,'day_desc':desc})
                date_start = date_start + datetime.timedelta(days=1)
            xls_wookbook = xlwt.Workbook()
            data_sheet = xls_wookbook.add_sheet(kwargs.get('date_start')+'~'+kwargs.get('date_stop')+'加班')

            style = xlwt.easyxf(
                'font: height 200;'
                'alignment: vert center, horizontal center;'
                'borders: left thin, right thin, top thin, bottom thin;'
            )
            overtime_type_ids = request.env['hr.overtime.type'].search([])
            ot_type_ids=[]
            for o in overtime_type_ids:
                ot_type_ids.append(o.name)


            header_list = ['部门','职工类型','工号','姓名','劳务公司']+descs+ot_type_ids
            [data_sheet.write(0, row, line, style) for row, line in enumerate(header_list)]

            current_row = 1
            for employee_id in employee_ids:
                employee_id=request.env['hr.employee'].browse(employee_id)
                employee_type=''
                if employee_id.employee_type=='1':
                    employee_type='月薪'
                elif employee_id.employee_type=='2':
                    employee_type = '日薪'
                elif  employee_id.employee_type=='3':
                    employee_type='小时工'
                data_sheet.write(current_row, 0, employee_id.department_id.name ,
                                 style)
                data_sheet.write(current_row, 1, employee_type,
                                 style)
                data_sheet.write(current_row, 2, employee_id.barcode,
                                 style)
                data_sheet.write(current_row, 3, employee_id.name,
                                 style)
                data_sheet.write(current_row, 4, employee_id.service_company_id.name if employee_id.service_company_id else '' ,
                                 style)
                for i,day in enumerate(days):
                    data_sheet.write(current_row, i+5, employee_id.get_employee_over_time_date(day),
                                     style)
                for j,type_id in enumerate(overtime_type_ids):
                    data_sheet.write(current_row, len(days) + 5+j, employee_id.get_employee_over_time_date_by_type(date_start_1,date_stop,type_id),
                                     style)



                # data_sheet.write(current_row, 1, vals.get('partner') and vals.get('partner') or '', style)
                # data_sheet.write(current_row, 2, vals.get('confirmation_date') and vals.get('confirmation_date') or '',
                #                  style)
                # data_sheet.write(current_row, 3, vals.get('product_id') and vals.get('product_id') or 0.0,
                #                  style)
                # data_sheet.write(current_row, 4, vals.get('team_id') and vals.get('team_id') or '', style)
                # data_sheet.write(current_row, 5, vals.get('area_id') and vals.get('area_id') or '',
                #                  style)
                # data_sheet.write(current_row, 6, vals.get('product_uom_qty') and vals.get('product_uom_qty') or 0.0,
                #                  style)
                # data_sheet.write(current_row, 7, vals.get('serial_id') and vals.get('serial_id') or '',
                #                  style)
                # data_sheet.write(current_row, 8, vals.get('sub_serial_id') and vals.get('sub_serial_id') or '',
                #                  style)
                # data_sheet.write(current_row, 9, vals.get('price_total') and vals.get('price_total') or 0.0,
                #                  style)
                # data_sheet.write(current_row, 10, vals.get('create_uid') and vals.get('create_uid') or 0.0,
                #                  style)

                current_row += 1
                i = 0

            for x, i in enumerate([2, 2, 2, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1]):
                data_sheet.col(x).width = 1280 * i


            # xls_wookbook.save(xls)
            # xls.seek(0)
            # content = xls.read()
            response= request.make_response(None, headers=[
                ('Content-Type', 'application/vnd.ms-excel'),
                ('Content-Disposition', 'attachment; filename=overtime.xls;')
            ])

            xls_wookbook.save(response.stream)
            return response
        else:
            raise UserError('没有相关资料可以导出')

